﻿#region 命名空间

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.UI.WebControls;
using WinnerCMS.BLL;
using WinnerCMS.Model;

#endregion

namespace WinnerCMS.WebUI.Admin.Member
{
    public partial class MemberRole : WebFactory.PageBase
    {
        protected string strOperation = "";

        private readonly RoleManage _myBLL = new RoleManage();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                var Action = Request["Action"];
                if (Action == null)
                {
                    this.GV_List.EmptyDataTemplate = GetTemplate(this);
                    BindGrid();
                    this.panel1.Visible = false;
                }
                else if (Action == "ReadOnly")
                {
                    this.btnAdd.Visible = false;
                    this.cbkALL.Visible = false;
                }
                else if (Action == "Add")
                {
                    this.GV_List.Visible = false;
                    this.panel1.Visible = true;
                    this.btnAdd.Text = "添加";
                    this.lblPath.Text = "<a href=\"Member_Role.aspx\">会员角色管理</a> → 添加角色";
                }
                else if (Action == "Edit")
                {
                    this.GV_List.Visible = false;
                    this.panel1.Visible = true;
                    this.btnAdd.Text = "修改";
                    this.lblPath.Text = "<a href=\"Member_Role.aspx\">会员角色管理</a> → 修改角色";
                }
                if (Action != null)
                {
                    if (Request["ID"] != null)
                    {
                        var model = this._myBLL.GetInfo(int.Parse(Request["ID"]));

                        this.txtRoleName.Text = model.RoleName;
                        this.NtxtRank.Text = model.Rank.ToString();

                        var rightIds = (from x in new RightsManage().GetRoleRights(model.Id) select x.Id).ToArray();
                        this.strOperation = string.Join(",", rightIds);
                    }
                    GetMemberPower();
                }
            }
        }

        private void BindGrid()
        {
            this.lblPath.Text = "<a href=\"Member_Role.aspx\">会员角色管理</a> ";
            this.GV_List.DataSource = this._myBLL.BindList(false);
            this.GV_List.DataBind();
        }

        private void GetMemberPower()
        {
            this.DataList1.DataSource = new RightsManage().GetRights(false);
            this.DataList1.DataBind();
        }

        protected IList<Right> BindPower(object rootId)
        {
            return new RightsManage().GetRights((int) rootId, false);
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            var str = Request.Form["chkOperation"];
            var model = new WinnerCMS.Model.Role();
            if (Request["Action"] == "Add")
            {
                model.RoleName = this.txtRoleName.Text;
                model.Rank = int.Parse(this.NtxtRank.Text);

                this._myBLL.Add(model, str);
            }
            else if (Request["Action"] == "Edit")
            {
                model.Id = int.Parse(Request["ID"]);
                model.RoleName = this.txtRoleName.Text;
                model.Rank = int.Parse(this.NtxtRank.Text);

                this._myBLL.Edit(model, str);
            }
        }

        protected void GV_List_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType != DataControlRowType.DataRow)
            {
                return;
            }
            TableCell myTableCell;
            //定义对“删除”单元格的引用
            myTableCell = e.Row.Cells[5];
            //定义对“删除”单元格中控件的引用
            var btnDel = (LinkButton) myTableCell.Controls[0];
            //添加当单击时弹出确认对话框的自定义属性
            btnDel.Attributes.Add("onclick", "return confirm('您真的要删除吗？属于该角色的会员要一起删掉的哦！');");
        }


        protected void GV_List_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            this._myBLL.Delete((int) this.GV_List.DataKeys[e.RowIndex].Value, false);

            BindGrid();
        }
    }
}